package com.anabur.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.anabur.persistent.Empresa;
import com.anabur.system.AafAbstractDAO;

/**
 * DAO implementador de los accesos a BBDD relacionados a empresas
 * @author Pablo
 *
 */
public class EmpresaDAO  extends AafAbstractDAO{
	
	public EmpresaDAO(SessionFactory sessionFac){
		super(sessionFac);
	}
	
	protected String getNameOfPojo() {
		return Empresa.class.getSimpleName();
	}
	
	public Empresa getEmpresaByCodigo(String codigo) throws Exception{
		Empresa empresa = null;
		Session sess = null;
		try{
			sess = sessionFac.openSession();
			Query query = sess.createQuery("from Empresa where codigo like :codigo");
			query.setString("codigo",codigo);
			empresa = (Empresa) query.uniqueResult();
			sess.close();
		}catch (Exception e) {
			sess.close();
			throw e;
		}		
		return empresa;
	}
	
	public List<Empresa> getEmpresaList() throws Exception{
		List<Empresa> list=null;
		Session sess = null;
		try{
			sess = sessionFac.openSession();
			Query query = sess.createQuery("from Empresa");
			list = (List) query.list();
			sess.close();
		}catch (Exception e) {
			sess.close();
			throw e;
		}	
		return list;
	}
	
	public List<Empresa> getEmpresaMervalList() throws Exception{
		List<Empresa> list=null;
		Session sess = null;
		try{
			sess = sessionFac.openSession();
			Query query = sess.createQuery("from Empresa where esMerval = true");
			list = (List) query.list();
			sess.close();
		}catch (Exception e) {
			sess.close();
			throw e;
		}	
		return list;
	}
	
	public List<Empresa> getEmpresaMerval25List() throws Exception{
		List<Empresa> list=null;
		Session sess = null;
		try{
			sess = sessionFac.openSession();
			Query query = sess.createQuery("from Empresa where esMerval25 = true");
			list = (List) query.list();
			sess.close();
		}catch (Exception e) {
			sess.close();
			throw e;
		}	
		return list;
	}

	public Integer getCantEmpresa() throws Exception{
		List<Empresa> list=null;
		Session sess = null;
		try{
			sess = sessionFac.openSession();
			Query query = sess.createQuery("from Empresa");
			list = (List) query.list();
			sess.close();
		}catch (Exception e) {
			sess.close();
			throw e;
		}	
		return list.size();
	}
}
